package com.be.beadmin.design.controller;


import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.design.dto.output.BomReviewEx;
import com.be.beadmin.design.dto.output.BomReviewExcelEx;
import com.be.beadmin.design.repository.BomReviewRepository;
import com.be.beadmin.design.service.DrawingReviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.design.service.BomReviewService;
import com.be.beadmin.design.dto.input.BomReviewQueryCriteria;
import com.be.beadmin.design.dto.input.BomReviewDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;
import org.springframework.web.multipart.MultipartFile;

/**
* @author zmy
* @date 2023-08-29
**/
@Api(tags = {"设计管理-Bom"})
@RestController

public class BomReviewController {


    @Autowired
    private  BomReviewService bomReviewService;

    @Autowired
    private DrawingReviewService drawingReviewService;


    @PostMapping(value = "/design/getBomReviewByPage")
    public ResultModel getBomReviewByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(bomReviewService.getBomReviewByPage(pageEntity)) ;
    }

    @ApiOperation("bom修改或者增加")
    @PostMapping("/design/addBomReview")
    public ResultModel addBomReview(@RequestBody BomReviewDto bomReviewDto) throws ParseException {
        bomReviewService.addBomReview(bomReviewDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/design/delBomReview")
    public ResultModel delBomReview(@RequestBody BomReviewDto bomReviewDto){
        bomReviewService.delBomReview(bomReviewDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @ApiOperation("bomreview删除")
    @PostMapping("/design/delBomReviewByIds")
    public ResultModel delBomReview(@ApiParam(value = "ids", required = true) @RequestParam List<String> ids){
        for(String id : ids) {
            bomReviewService.delBomReview(id);
        }
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/design/updateBomReview")
    public ResultModel updateBomReview(@RequestBody BomReviewDto bomReviewDto){
        bomReviewService.updateBomReview(bomReviewDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/design/exportBomReview")
    public void exportBomReview(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<BomReviewEx> bomReviewPage = bomReviewService.getBomReviewByPage(pageEntity);
        List<BomReviewEx> bomReviewExes = bomReviewPage.getContent();
        List<BomReviewExcelEx> bomReviewExcelExes=bomReviewExes.stream().map(item->{
            BomReviewExcelEx excel=new BomReviewExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("BomReview接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), BomReviewExcelEx.class).sheet("sheet1").doWrite(bomReviewExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), BomReviewExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(bomReviewExcelExes);
    }

    @ApiOperation("电子看板-获取未审批的数量（BOM+图纸）")
    @PostMapping("/product/getUnReviewedProgress")
    public ResultModel getUnReviewedProgress(){
        ArrayList<Integer> unReviewedtProgress = new ArrayList<>();
        int bomRes = bomReviewService.countUnreviewedBom();
        int drawingRes = drawingReviewService.countUnreviewedDrawing();

        return ResultModel.success(bomRes+drawingRes);
    }




//放到公共模块
//    @ApiOperation("确认审核,通过=2；驳回=3")
//    @PostMapping("/design/confirmBomReview")
//    public ResultModel confirmBomReview(
//            @ApiParam(value = "id", required = true) @RequestParam String id,
//            @ApiParam(value = "审核状态", required = true) @RequestParam int reviewState,
//            @ApiParam(value = "审核备注") @RequestParam(required = false) String reviewMemo) throws ParseException {
//
//            bomReviewService.confirmBomReview(id,reviewState,reviewMemo);
//        return ResultModel.success("审核完成！");
//    }

    @Autowired
    private BomReviewRepository bomReviewRepository;
    @ApiOperation("bomreview提交")
    @PostMapping("/design/postBomReviewByIds")
    public ResultModel postBomReviewByIds(@ApiParam(value = "ids") @RequestParam(required = false) List<String> ids,
                                          @ApiParam(value = "withoutpostids", required = false) @RequestParam(required = false) List<String> withoutPostIds
    ){
        if(ids != null) {
            for (String id : ids) {
                bomReviewService.postBomReview(id);
            }
        }
        if(withoutPostIds != null) {
            for (String id : withoutPostIds) {
                bomReviewRepository.deleteById(id);
            }
        }
        return ResultModel.success(ResultModel.UPDATE);
    }
}